package com.admin.web;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.admin.model.FileMeta;
import com.admin.service.AdminCTSService;
import com.admin.service.AdminSchoolService;
import com.common.model.AdminUser;
import com.common.model.ClassInfo;
import com.common.model.College;
import com.common.model.Department;
import com.common.model.StudentUser;
import com.common.model.TeacherUser;

@Controller
@RequestMapping("/admin/teacher")
public class AdminTeacherWeb {
	@Autowired
	AdminSchoolService schooService;
	@Autowired
	AdminCTSService ctsService;
	/**
	 * 根据条件查询教师
	 * @param clgId 学院id
	 * @param depId 系id
	 * @param page 页码(从1开始)
	 * @return
	 */
	@RequestMapping("/showTeacherList/{clg_id}/{dep_id}/{page}")
	public ModelAndView showTeacher(
			@PathVariable("clg_id") int clgId,
			@PathVariable("dep_id") int depId,
			@PathVariable("page")int page){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("admin/cts/showTeacher");
		mav.addObject("colleges", schooService.getAllCollege());
		int minClgId=schooService.getMinCollegeId();
		if(clgId<minClgId){
			clgId=minClgId;
		}
		mav.addObject("departments", schooService.getDepsOfCollege(clgId));
		int minDepId=schooService.getMinDepIdOfCollege(clgId);
		if(depId<minDepId){
			depId=minDepId;
		}
		mav.addObject("classList",ctsService.getClassesByDepId(depId));
		int teacherCount=schooService.getTeacherCountByDepId(depId);
		int totalPage=(int)Math.ceil(teacherCount/10.0);
		if(totalPage==0){
			totalPage=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		mav.addObject("clgId", clgId);
		mav.addObject("depId", depId);
		mav.addObject("page", page);
		mav.addObject("teacher",ctsService.getTeachersByDepId(depId, page));
		mav.addObject("teacherCount",teacherCount );
		mav.addObject("totalPage",totalPage);
		mav.addObject("currentMenu", "showTeacherList");
		return mav;
	}
	/**
	 * 显示修改教师信息界面
	 * @param stuid
	 * @return
	 */
	@RequestMapping("/showUpdateTeacher/{tea_id}")
	public ModelAndView showUpdateStudent(@PathVariable("tea_id")int teaId,HttpSession session){
		ModelAndView mav=new ModelAndView();
		TeacherUser teacher=ctsService.getTeacherByTeacherId(teaId);
		AdminUser admin=(AdminUser)session.getAttribute("user");
		int teaClgId=teacher.getDepartment().getCollege().getId();
		if(!(admin.isSuperAdmin()||admin.getCollegeId()==teaClgId)){
			mav.setViewName("common/error");
			mav.addObject("error", "该教师并不是您学院的,请勿非法操作");
			return mav;
		}
		mav.setViewName("admin/cts/updateTeacher");
		mav.addObject("colleges", schooService.getAllCollege());
		mav.addObject("departments", schooService.getDepsOfCollege(teaClgId));
		mav.addObject("teaClgId", teaClgId);
		mav.addObject("teacher", teacher);
		mav.addObject("currentMenu", "showTeacherList");
		return mav;
	}
	/**
	 * 修改学生
	 * @param teacher
	 * @param session
	 * @return -1参数不正确,-2 权限不足,-3重复no,-4参数错误2
	 */
	@ResponseBody
	@RequestMapping("/updateTeacher")
	public int updateStudent(TeacherUser teacher,HttpSession session){
		if(teacher.getId()==null||teacher.getDepartmentId()==null){
			return -1;
		}
		int orgClgId=ctsService.getTeacherByTeacherId(teacher.getId()).getDepartment().getCollege().getId();
		int newClgId=schooService.getDepartmentByDepId(teacher.getDepartmentId()).getCollege().getId();
		AdminUser admin =(AdminUser)session.getAttribute("user");
		if(!(admin.isSuperAdmin()||(admin.getCollegeId()==orgClgId&&admin.getCollegeId()==newClgId))){
			return -2;
		}
		int result= ctsService.updateTeacher(teacher);
		switch (result) {
		case -1:
			return -1;
		case -2:
			return -3;
		case -3:
			return -4;
		default:
			return 0;
		}
	}
	/**
	 * 重设教师密码
	 * @param password
	 * @param teaId
	 * @param session
	 * @return -1无权限 -2参数错误
	 */
	@ResponseBody
	@RequestMapping("/resetTeacherPassword")
	public int resetTeacherPassword(String password,int teaId,HttpSession session){
		int orgClgId=ctsService.getTeacherByTeacherId(teaId).getDepartment().getCollege().getId();
		AdminUser admin =(AdminUser)session.getAttribute("user");
		if(!(admin.isSuperAdmin()||admin.getCollegeId()==orgClgId)){
			return -1;
		}
		int result=ctsService.resetTeacherPassword(password, teaId);
		if(result==-1){
			return -2;
		}
		return 0;
	}
	@RequestMapping("/showInsertTeacher")
	public ModelAndView showInsertTeacher(HttpSession session){
		ModelAndView mav=new ModelAndView();
		AdminUser admin=(AdminUser)session.getAttribute("user");
		ArrayList<College> allClgs=schooService.getAllCollege();
		ArrayList<Department>deps=null;
		if(admin.isSuperAdmin()){
			deps=schooService.getDepsOfCollege(allClgs.get(0).getId());
		}else{
			deps=schooService.getDepsOfCollege(admin.getCollegeId());
		}
		mav.setViewName("admin/cts/insertTeacher");
		mav.addObject("colleges",allClgs );
		mav.addObject("departments",deps);
		mav.addObject("currentMenu", "showTeacherList");
		return mav;
	}
	/**
	 * 增加一个教师
	 * @param student
	 * @param session
	 * @return -1 参数错误 -2无权限 -3重复no -4参数错误2;
	 */
	@ResponseBody
	@RequestMapping("/insertTeacher")
	public int insertTeacher(TeacherUser teacher,HttpSession session){
		if(teacher.getDepartmentId()==null){
			return -1;
		}
		AdminUser admin=(AdminUser)session.getAttribute("user");
		int newClgId=schooService.getDepartmentByDepId(teacher.getDepartmentId()).getCollege().getId();
		if(!(admin.isSuperAdmin()||admin.getCollegeId()==newClgId)){
			return -2;
		}
		int result=ctsService.insertTeacher(teacher);
		switch (result) {
		case -1:
			return -1;
		case -2:
			return -3;
		case -3:
			return -4;
		default:
			return 0;
		}
	}
	/**
	 * 由excel导入学生数据
	 * @param excel
	 * @param session
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/insertTeacherByExcel")
	public ArrayList<FileMeta> insertTeacherByExcel(MultipartFile excel,HttpSession session) throws IOException{
		FileMeta file=new FileMeta();
		file.setFileName(excel.getOriginalFilename().toLowerCase());
		file.setFileSize(excel.getSize()/1024.0+"kb");
		file.setFileType(excel.getContentType());
		file.setInputStresm(excel.getInputStream());
		ctsService.insertTeacherByExcel(file, session);
		ArrayList<FileMeta> arr=new ArrayList<FileMeta>();
		arr.add(file);
		return arr;
	}
	@RequestMapping("/showInsertReport")
	public ModelAndView showInsertByExcelResult( HttpSession session){
		ModelAndView mav=new ModelAndView();
		if(session.getAttribute("teacherExcelReadResult")==null){
			mav.setViewName("redirect:showTeacherList/1/1/1");
			return mav;
		}
		mav.setViewName("admin/cts/importTeacherReport");
		mav.addObject("report", session.getAttribute("teacherExcelReadResult"));
		mav.addObject("successCount", session.getAttribute("teacherExcelReadSuccessCount"));
		session.removeAttribute("teacherExcelReadResult");
		session.removeAttribute("teacherExcelReadSuccessCount");
		mav.addObject("currentMenu", "showTeacherList");
		return mav;
	};
	/**
	 * 删除教师
	 * @param teaId
	 * @param session
	 * @return -1无权限 -2毕业设计
	 */
	@ResponseBody
	@RequestMapping("/deleteTeacher/{tea_id}")
	public int deleteTeacher(@PathVariable("tea_id")int teaId,HttpSession session){
		int orgClgId=ctsService.getTeacherByTeacherId(teaId).getDepartment().getCollege().getId();
		AdminUser admin =(AdminUser)session.getAttribute("user");
		if(!(admin.isSuperAdmin()||admin.getCollegeId()==orgClgId)){
			return -1;
		}
		int result=ctsService.deleteTeacher(teaId);
		if(result==0){
			return 0;
		}
		return result-1;
	}
	/**
	 * 查询指定系下所有的教师
	 * @param depId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getAllTeacherByDep/{dep_id}")
	public ArrayList<TeacherUser> getAllTeacherOfDep(@PathVariable("dep_id")int depId){
		return ctsService.getAllTeacherOfDep(depId);
	}
}
